package net.wanho.customerManage.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.customerManage.dao.LinkManDAO;
import net.wanho.customerManage.po.LinkMan;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LinkManDAOImpl extends BaseDAOImpl<LinkMan> implements LinkManDAO {
    @Override
    public List<LinkMan> selectAll() throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  LINK_MAIN_ID linkMainId,\n" +
                    "  NAME name,\n" +
                    "  SEX sex,\n" +
                    "  POSITION position,\n" +
                    "  NICKNAME nickname,\n" +
                    "  PHONENUM phonenum,\n" +
                    "  EMAIL email,\n" +
                    "  QQ qq,\n" +
                    "  CREATE_TIME createTime,\n" +
                    "  UPDATE_TIME updateTime,\n" +
                    "  REMARKS remarks,\n" +
                    "  QR_CODE qrode,\n" +
                    "  CUSTOMER_ID customerId\n" +
                    "FROM link_man";
            Object[] params = {};
            return super.queryForList(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean insert(LinkMan linkMan) throws DAOException {
        try {
            String sql = "INSERT INTO link_man (\n" +
                    "  LINK_MAIN_ID,\n" +
                    "  NAME,\n" +
                    "  SEX,\n" +
                    "  POSITION,\n" +
                    "  NICKNAME,\n" +
                    "  PHONENUM,\n" +
                    "  EMAIL,\n" +
                    "  QQ,\n" +
                    "  CREATE_TIME,\n" +
                    "  UPDATE_TIME,\n" +
                    "  REMARKS,\n" +
                    "  QR_CODE,\n" +
                    "  CUSTOMER_ID\n" +
                    ") \n" +
                    "VALUES\n" +
                    "  (NULL, ?, ?, ?, ?, ?, ?, ?, now(),now(), ?, ?, ?)";
            Object[] params = {linkMan.getName(), linkMan.getSex(), linkMan.getPosition(), linkMan.getNickname(), linkMan.getPhonenum(),
                    linkMan.getEmail(), linkMan.getQq(), linkMan.getRemarks(), linkMan.getQrode(), linkMan.getCustomerId()};
            return super.jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<LinkMan> selectByCond(Map<String, String> condMap) throws DAOException {
        try {
            //基础SQL
            String sql = "SELECT\n" +
                    "  LINK_MAIN_ID linkMainId,\n" +
                    "  NAME name,\n" +
                    "  SEX sex,\n" +
                    "  POSITION position,\n" +
                    "  NICKNAME nickname,\n" +
                    "  PHONENUM phonenum,\n" +
                    "  EMAIL email,\n" +
                    "  QQ qq,\n" +
                    "  CREATE_TIME createTime,\n" +
                    "  UPDATE_TIME updateTime,\n" +
                    "  REMARKS remarks,\n" +
                    "  QR_CODE qrode,\n" +
                    "  CUSTOMER_ID customerId\n" +
                    "FROM link_man WHERE 1=1 ";
            //动态拼接SQL
            StringBuffer sb = new StringBuffer(sql);
            //条件无效  或者 没有条件
            boolean isCond = condMap == null || condMap.isEmpty();
            //创建集合 存放 动态参数值 ？ 替换的值
            List<Object> condParamValueList = new ArrayList<>();
            //有效条件
            if (!isCond) {
                condMap.forEach((k, v) -> {
                    sb.append(" AND ").append(k).append(" LIKE  ?");
                    condParamValueList.add("%" + v + "%");
                });
            }
            Object[] params = condParamValueList.toArray();
            return super.queryForList(sb.toString(), params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public LinkMan findById(String id) throws DAOException {
        try {
            String sql = "SELECT\n" +
                    "  LINK_MAIN_ID linkMainId,\n" +
                    "  NAME name,\n" +
                    "  SEX sex,\n" +
                    "  POSITION position,\n" +
                    "  NICKNAME nickname,\n" +
                    "  PHONENUM phonenum,\n" +
                    "  EMAIL email,\n" +
                    "  QQ qq,\n" +
                    "  CREATE_TIME createTime,\n" +
                    "  UPDATE_TIME updateTime,\n" +
                    "  REMARKS remarks,\n" +
                    "  QR_CODE qrode,\n" +
                    "  CUSTOMER_ID customerId\n" +
                    "FROM link_man WHERE LINK_MAIN_ID = ? ";
            Object[] params = {id};
            return super.queryForObject(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean update(LinkMan linkMan) throws DAOException {
        try {
            String sql = "UPDATE link_man\n" +
                    "SET NAME = ?,\n" +
                    "  SEX = ?,\n" +
                    "  POSITION = ?,\n" +
                    "  NICKNAME = ?,\n" +
                    "  PHONENUM = ?,\n" +
                    "  EMAIL = ?,\n" +
                    "  QQ = ?,\n" +
                    "  UPDATE_TIME = NOW(),\n" +
                    "  REMARKS = ?\n" +
                    "WHERE LINK_MAIN_ID = ?";
            Object[] params = {linkMan.getName(), linkMan.getSex(), linkMan.getPosition(), linkMan.getNickname(),
                    linkMan.getPhonenum(), linkMan.getEmail(), linkMan.getQq(), linkMan.getRemarks(), linkMan.getLinkMainId()};
            return super.jdbcTemplate.update(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }
}
